<script setup>

//删除账号
import {ElMessage, ElMessageBox} from "element-plus";
import {onMounted, ref} from "vue";
import http from "@/request/Http.js";
import {useRouter} from "vue-router";

//id获取当前id
const userId = ref()
const router = useRouter();


const handleClickDelete = () => {
  ElMessageBox.confirm(
      `你确定要注销账号吗?`,
      {
        confirmButtonText: "确定注销",
        cancelButtonText: "取消",
        type: 'warning'
      }
  ).then(async () => {
    const data = await http.post("/user/deleteUser", {userId: userId.value});
    if (data.code === 200) {
      ElMessage.success("注销成功!")
      //令token变为空,推到登录页面，未登录不能访问
      sessionStorage.setItem("token", '');
      //跳转到登录页面
      router.push('/login')
    } else {
      ElMessage.error("删除失败!" + data.msg)
    }
  })
}

// 获取对象信息
const getUserMessage = async () => {
  const data = await http.get("/user/userMessage")
  console.log(data.data.id)
  userId.value = data.data.id
  console.log(userId.value)
}
onMounted(async () => {
  await getUserMessage()
})

</script>

<template>
  <div>
    <p>注销账号</p>
    <el-button @click="handleClickDelete">注销</el-button>
  </div>
</template>

<style scoped>

</style>